package com.zl.Sqlite.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.zl.Sqlite.DBheloper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * User: c3t
 * Date: 14-5-2
 * Time: 上午4:36
 */
public class SQLiteTemplateByAPI {
    private DBheloper dBheloper;

    public SQLiteTemplateByAPI(Context context) {
        dBheloper = DBheloper.getInstance(context);
    }

    private void executeRead(MyCallback myCallback) {
        SQLiteDatabase db = dBheloper.getReadableDatabase();
        myCallback.doTemplate(db);
        db.close();
    }

    private void executeWrite(MyCallback myCallback) {
        SQLiteDatabase db = dBheloper.getWritableDatabase();
        myCallback.doTemplate(db);
        db.close();
    }

    /**
     * 添加
     *
     * @param map 要添加的列和值对
     */
    public ResulstObject add(final Map map, final String tableName) {
        final ResulstObject resulstObject = new ResulstObject();
        executeWrite(new MyCallback() {
            @Override
            public void doTemplate(SQLiteDatabase db) {
                ContentValues values = new ContentValues();
                switchType(values, map);
                resulstObject.setExist((db.insert(tableName, null, values) > 0 ? true : false));
            }
        });
        return resulstObject;
    }

    private void switchType(ContentValues values, Map map) {
        Set keys = map.keySet();
        for (Object key : keys) {
            if (map.get(key) instanceof String) {
                values.put(String.valueOf(key), String.valueOf(map.get(key)));
            } else if (map.get(key) instanceof Byte) {
                values.put(String.valueOf(key), Byte.valueOf(String.valueOf(map.get(key))));
            } else if (map.get(key) instanceof Short) {
                values.put(String.valueOf(key), Short.valueOf(String.valueOf(map.get(key))));
            } else if (map.get(key) instanceof Integer) {
                values.put(String.valueOf(key), Integer.valueOf(String.valueOf(map.get(key))));
            } else if (map.get(key) instanceof Long) {
                values.put(String.valueOf(key), Long.valueOf(String.valueOf(map.get(key))));
            } else if (map.get(key) instanceof Float) {
                values.put(String.valueOf(key), Float.valueOf(String.valueOf(map.get(key))));
            } else if (map.get(key) instanceof Double) {
                values.put(String.valueOf(key), Double.valueOf(String.valueOf(map.get(key))));
            } else if (map.get(key) instanceof Boolean) {
                values.put(String.valueOf(key), Boolean.valueOf(String.valueOf(map.get(key))));
            } else if (map.get(key) instanceof Byte[]) {
                values.put(String.valueOf(key), (String.valueOf(map.get(key))).getBytes());
            }
        }
    }

    /**
     * 判断用户是否存在
     */
    public ResulstObject find(final String name, final String tableName) {
        final ResulstObject resulstObject = new ResulstObject();
        executeRead(new MyCallback() {
            @Override
            public void doTemplate(SQLiteDatabase db) {
                Cursor cursor = db.query(tableName, null, "name=?", new String[]{name}, null, null, null);
                boolean exist = cursor.moveToNext();
                cursor.close();
                resulstObject.setExist(exist);
            }
        });
        return resulstObject;
    }

    /**
     * 更新
     */
    public ResulstObject update(final Map map, final String tableName, final String whereCaluse, final String[] whereargs) {
        final ResulstObject resulstObject = new ResulstObject();
        executeWrite(new MyCallback() {
            @Override
            public void doTemplate(SQLiteDatabase db) {
                //values包含要更新的数据
                ContentValues values = new ContentValues();
                switchType(values, map);
                int result = db.update(tableName, values, whereCaluse, whereargs);
                resulstObject.setExist((result > 0 ? true : false));
            }
        });
        return resulstObject;
    }

    /**
     * 删除
     */
    public ResulstObject delete(final String tableName, final String whereCaluse, final String[] whereargs) {
        final ResulstObject resulstObject = new ResulstObject();
        executeWrite(new MyCallback() {
            @Override
            public void doTemplate(SQLiteDatabase db) {
                int result = db.delete(tableName, whereCaluse, whereargs);
                resulstObject.setExist((result > 0 ? true : false));

            }
        });
        return resulstObject;
    }

    /**
     * 得到所有用户
     */
    public ResulstObject findAll(final String tableName,final String[] columns,final String selections,final String[] selectionArgs,final String groupby ,final String having,final String orderby) {
        final ResulstObject resulstObject = new ResulstObject();
        executeRead(new MyCallback() {
            @Override
            public void doTemplate(SQLiteDatabase db) {
                ArrayList<Object> rs = new ArrayList<>();
                //Cursor cursor = db.rawQuery(sql, null);
                Cursor cursor = db.query(tableName,columns,selections,selectionArgs,groupby,having,orderby);
                while (cursor.moveToNext()) {
                    Map<Object, Object> map = new HashMap<>();
                    for(int i=0;i<columns.length;i++){
                        String value = cursor.getString(cursor.getColumnIndex(columns[i]));
                        map.put(columns[i], value);
                    }
                    rs.add(map);
                }
                resulstObject.setResult(rs);
            }
        });
        return resulstObject;
    }
}
